package dk.nordahl.todolist;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class ToDoListMainActivity extends Activity implements NewItemFragment.OnNewItemAddedListener{
	private ToDoItemAdapter aa;
	private ArrayList<ToDoItem> todoItems;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_list_main);
    FragmentManager fm = getFragmentManager();
    ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);
    
    
    todoItems = new ArrayList<ToDoItem>();
    int resID = R.layout.todolist_item;
    aa = new ToDoItemAdapter(this, resID, todoItems);
    todoListFragment.setListAdapter(aa);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.to_do_list_main, menu);
        
       
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId())
    	{
    	case R.id.action_settings:
    		Toast tt = Toast.makeText(this, "Test", Toast.LENGTH_SHORT);
    		tt.show();
    		break;
    	case R.id.menu_about:
    		Toast.makeText(this, "About", Toast.LENGTH_SHORT).show();
    		break;
    		
    		default:
    			break;
    			
    	}
    	return super.onOptionsItemSelected(item);
    }
	@Override
	public void onNewItemAdded(String newItem) {
		
		ToDoItem newToDoItem = new ToDoItem(newItem);
		todoItems.add(0,newToDoItem);
		aa.notifyDataSetChanged();
	}

}